package com.liang;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * ScheduledExecutorService
 * 一个 ExecutorService，可安排在给定的延迟后运行或定期执行的命令。
 *
 * @author: Liangxp
 * @date: 2019/8/3 15:17
 */
public class TestScheduleThreadPool {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);

        // 为线程池分配5个10秒后开始执行的任务
        for (int i = 0; i < 5; i++) {
            pool.schedule(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int num = new Random().nextInt(100);
                    System.out.println(Thread.currentThread().getName() + ":" + num);
                    return num;
                }
            }, 10, TimeUnit.SECONDS);
        }


        pool.shutdown();
    }
}
